package com.mysoft.gfyx.marketdevelop.service.impl;

import com.mysoft.framework.common.exception.BusinessLogicException;
import com.mysoft.framework.common.util.MappingUtil;
import com.mysoft.gfyx.common.rpc.opptyservice.OpptyServiceClientFactoryService;
import com.mysoft.gfyx.common.rpc.opptyservice.OpptyServiceClientService;
import com.mysoft.gfyx.common.rpc.opptyservice.dto.*;
import com.mysoft.gfyx.marketdevelop.controller.dto.ContactCooperationListRequestDto;
import com.mysoft.gfyx.marketdevelop.controller.dto.ContactListRequestDto;
import com.mysoft.gfyx.marketdevelop.controller.dto.GetDetailDataRequestDto;
import com.mysoft.gfyx.marketdevelop.service.ContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class ContactServiceImp implements ContactService {

    @Autowired
    private OpptyServiceClientFactoryService opptyServiceClientFactoryService;

    public List<ContactListRequestDto.Company> distinctName(List<ContactListRequestDto.Company> companyList) {
        List<ContactListRequestDto.Company> newCompany = new ArrayList<>();
        Set<String> companySet = new HashSet<>();
        companyList.forEach(r -> {
            ContactListRequestDto.Company item = new ContactListRequestDto.Company();
            item.setCompany_name(r.getCompany_name());
            item.setOrganizing_code(r.getOrganizing_code());
            if (!companySet.contains(r.getCompany_name())) {
                newCompany.add(item);
            }
            companySet.add(r.getCompany_name());
        });
        return newCompany;
    }

    public Object getContactList(ContactListRequestDto dto) {
        if (dto.getCompany().size() > 0 && (dto.getCompany().get(0).getCompany_name() == null || dto.getCompany().get(0).getCompany_name().equals("")) && (dto.getCompany().get(0).getOrganizing_code() == null || dto.getCompany().get(0).getOrganizing_code().equals(""))) {
            return new HashMap<>();
        }
        OpptyServiceClientService opptyService = opptyServiceClientFactoryService.getOpptyServiceClientService();
        com.mysoft.gfyx.common.rpc.opptyservice.dto.ContactListRequestDto requestDto = MappingUtil.getSimpleMapperFacade().map(dto, com.mysoft.gfyx.common.rpc.opptyservice.dto.ContactListRequestDto.class);
        ContactListResponseDto res = opptyService.getContactList(requestDto);

        //中标特殊处理
        if (requestDto.getCompany().size() == 4) {
            return this.getWinBidContact(dto, res);
        }

        int developerNum = 0;
        //全部 = 甲方联系人+多乙方
        if (requestDto.getContact_type().equals("developer_and_supplier") && (!requestDto.getCompany().get(0).getCompany_name().equals("") || !requestDto.getCompany().get(0).getOrganizing_code().equals(""))) {
            developerNum += 1;
            ContactListResponseDto.CooperationCompany cooperationCompany = new ContactListResponseDto.CooperationCompany();
            cooperationCompany.setCompany_type("developer");
            cooperationCompany.setCooperation_company_name(requestDto.getCompany().get(0).getCompany_name());
            cooperationCompany.setCooperation_organizing_code(requestDto.getCompany().get(0).getOrganizing_code());
            cooperationCompany.setContact(res.getData().getContact_list());
            cooperationCompany.setContact_cnt(res.getData().getContact_total());
            if (cooperationCompany.getContact() != null && cooperationCompany.getContact().size() > 2) {
                cooperationCompany.setContact(cooperationCompany.getContact().subList(0, 2));
            }

            List<ContactListResponseDto.CooperationCompany> newCooperationCompany = new ArrayList<>();
            newCooperationCompany.add(cooperationCompany);
            if (res.getData().getCooperation_company_total() > 0) {
                newCooperationCompany.addAll(res.getData().getCooperation_company());
            }
            res.getData().setCooperation_company(newCooperationCompany);
        }
        Map<String, Object> response = new HashMap<>();
        switch (dto.getContact_type()) {
            case "single_developer":
            case "single_supplier":
                response.put("list", res.getData().getContact_list());
                response.put("total", res.getData().getContact_total());
                break;
            case "multi_supplier":
                response.put("list", res.getData().getCooperation_company());
                response.put("total", res.getData().getCooperation_company_total());
                break;
            case "developer_and_supplier":
                response.put("list", res.getData().getCooperation_company());
                response.put("total", res.getData().getCooperation_company_total() + developerNum);
                break;
            default:
                break;
        }
        return response;
    }

    public Map<String, Object> getWinBidContact(ContactListRequestDto dto, ContactListResponseDto res) {
        dto.setCompany(this.distinctName(dto.getCompany()));//公司去重

        Map<String, Object> response = new HashMap<>();
        if (dto.getContact_type().equals("single_developer")) {
            response.put("list", res.getData().getContact_list());
            response.put("total", res.getData().getContact_total());
        } else {
            List<ContactListResponseDto.CooperationCompany> cooperationCompany = new ArrayList<>();
            AtomicInteger cooperationCompanyTotal = new AtomicInteger();
            int startNum = dto.getContact_type().equals("developer_and_supplier") ? 0 : 1;
            dto.getCompany().forEach(r -> {
                if (!r.getCompany_name().equals("")) {
                    ContactListResponseDto.CooperationCompany item = new ContactListResponseDto.CooperationCompany();
                    item.setCooperation_company_name(r.getCompany_name());
                    item.setCooperation_organizing_code(r.getOrganizing_code());
                    //联系人
                    List<ContactListResponseDto.ContactList> contactList = new ArrayList<>();
                    if (res.getData().getContact_list() != null && res.getData().getContact_list().size() > 0) {
                        res.getData().getContact_list().forEach(p -> {
                            if (p.getCompany_name().equals(r.getCompany_name())) {
                                contactList.add(p);
                                item.setContact_cnt(item.getContact_cnt() + 1);
                            }
                        });
                    }

                    if (contactList.size() > 2) {
                        item.setContact(contactList.subList(0, 2));
                    } else {
                        item.setContact(contactList);
                    }

                    if (dto.getContact_type().equals("developer_and_supplier") && cooperationCompanyTotal.intValue() == 0) {
                        item.setCompany_type("developer");
                    } else {
                        item.setCompany_type("supplier");
                    }
                    if (cooperationCompanyTotal.intValue() >= startNum) {
                        cooperationCompany.add(item);
                    }
                    cooperationCompanyTotal.set(cooperationCompanyTotal.get() + 1);
                }


            });
            response.put("list", cooperationCompany);
            response.put("total", cooperationCompany.size());
        }
        return response;
    }

    public ContactListResponseDto.ContactList contactDetail(GetDetailDataRequestDto dto) {
        OpptyServiceClientService opptyService = opptyServiceClientFactoryService.getOpptyServiceClientService();
        com.mysoft.gfyx.common.rpc.opptyservice.dto.ContactDetailRequestDto requestDto = MappingUtil.getSimpleMapperFacade().map(dto, com.mysoft.gfyx.common.rpc.opptyservice.dto.ContactDetailRequestDto.class);
        ContactDetailResponseDto res = opptyService.contactDetail(requestDto);
        ContactListResponseDto.ContactList resp = new ContactListResponseDto.ContactList();
        if (res.getData().getList() != null && res.getData().getList().size() > 0) {
            return res.getData().getList().get(0);
        } else {
            throw new BusinessLogicException("未找到相关联系人");
        }
    }

    public ContactListResponseDto.ContactList getMobile(GetDetailDataRequestDto dto) {
        OpptyServiceClientService opptyService = opptyServiceClientFactoryService.getOpptyServiceClientService();
        com.mysoft.gfyx.common.rpc.opptyservice.dto.ContactGetMobileRequestDto requestDto = MappingUtil.getSimpleMapperFacade().map(dto, com.mysoft.gfyx.common.rpc.opptyservice.dto.ContactGetMobileRequestDto.class);
        ContactGetMobileResponseDto res = opptyService.getMobile(requestDto);
        return res.getData();
    }

    public ContactCooperationListResponseDto.CooperationList cooperationList(ContactCooperationListRequestDto dto) {
        OpptyServiceClientService opptyService = opptyServiceClientFactoryService.getOpptyServiceClientService();
        com.mysoft.gfyx.common.rpc.opptyservice.dto.ContactCooperationListRequestDto requestDto = MappingUtil.getSimpleMapperFacade().map(dto, com.mysoft.gfyx.common.rpc.opptyservice.dto.ContactCooperationListRequestDto.class);
        return opptyService.cooperationList(requestDto).getData();
    }

    public RpaContactResponseDto.DataList rpaContact(RpaContactRequestDto dto) {
        dto.setPage(dto.getPage());
        dto.setPage_size(dto.getPage_size());
        dto.setShow_view_times(true);
        OpptyServiceClientService opptyService = opptyServiceClientFactoryService.getOpptyServiceClientService();
        RpaContactResponseDto res = opptyService.rpaContact(dto);
        if (res.getData().getTotal() == 0) {
            res.getData().setList(new ArrayList<>());
        }
        return res.getData();

        /*
        //按角色分组
        List<RpaContactResponseDto.RoleList> roleList = new ArrayList<>();
        res.getData().getList().forEach(d -> {
            AtomicBoolean roleExist = new AtomicBoolean(false);
            roleList.forEach(r -> {
                if (r.getRole_name().equals(d.getPerson_role())) {
                    List<RpaContactResponseDto.ContactList> contactList = r.getList();
                    RpaContactResponseDto.ContactList contactRow = new RpaContactResponseDto.ContactList();
                    contactRow.setCompany_name(d.getCompany_name());
                    contactRow.setCompany_address(d.getCompany_address());
                    contactRow.setId(d.getId());
                    contactRow.setPerson_phone(d.getPerson_phone());
                    contactRow.setPerson_name(d.getPerson_name());
                    contactRow.setPerson_position(d.getPerson_position());
                    contactRow.setPerson_sexy(d.getPerson_sexy());
                    contactRow.setPerson_role(d.getPerson_role());
                    contactRow.setProject_id(d.getProject_id());
                    contactRow.setRcc_project_name(d.getRcc_project_name());
                    contactRow.setSrc_id(d.getSrc_id());
                    contactRow.setView_times(d.getView_times());
                    contactRow.setSource_type(d.getSource_type());
                    contactList.add(contactRow);
                    r.setList(contactList);
                    roleExist.set(true);
                }
            });

            if (!roleExist.get() && !d.getPerson_role().equals("")) {
                RpaContactResponseDto.RoleList role = new RpaContactResponseDto.RoleList();
                role.setRole_name(d.getPerson_role());

                List<RpaContactResponseDto.ContactList> contactList = new ArrayList<>();
                RpaContactResponseDto.ContactList contactRow = new RpaContactResponseDto.ContactList();
                contactRow.setCompany_name(d.getCompany_name());
                contactRow.setCompany_address(d.getCompany_address());
                contactRow.setId(d.getId());
                contactRow.setPerson_phone(d.getPerson_phone());
                contactRow.setPerson_name(d.getPerson_name());
                contactRow.setPerson_position(d.getPerson_position());
                contactRow.setPerson_sexy(d.getPerson_sexy());
                contactRow.setPerson_role(d.getPerson_role());
                contactRow.setProject_id(d.getProject_id());
                contactRow.setRcc_project_name(d.getRcc_project_name());
                contactRow.setSrc_id(d.getSrc_id());
                contactRow.setView_times(d.getView_times());
                contactRow.setSource_type(d.getSource_type());
                contactList.add(contactRow);
                role.setList(contactList);
                roleList.add(role);
            }
        });
        roleList.forEach(p -> {
            p.setContact_num(p.getList().size());
        });
        return roleList;

         */

    }

    public RpaContactMobileResponseDto rpaContactMobile(RpaContactMobileRequestDto dto) {
        OpptyServiceClientService opptyService = opptyServiceClientFactoryService.getOpptyServiceClientService();
        RpaContactMobileResponseDto res = opptyService.rpaContactMobile(dto);
        return res;
    }

}
